<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 合同管理
 */
namespace app\content\controller;

use  Mscore\Core\Controller;
use  app\content\model\Contract as ContractModel;
use  app\content\model\Common as CommonModel;
use  Mscore\Core\Lib\ArrayMethod;
use  Mscore\Core\Lib\Upload;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lang;


class ContractController extends Controller{

    private $model;
    private $login;
    private $error=['code'=>0,'msg'=>'fail'];
    private $success=['code'=>200,'msg'=>'success']; 
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->model = new ContractModel;
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    /**
     * 合同类别设置列表
     * @param array $catelist 
     * @param array $where 
     */ 
    public function catelist(){
        $post = post();
        switch (true) {
            case $post:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' dels<1 ';
                if(post('title')){
                    $where .= ' and title like "%'.post('title').'%"';
                }
                $datalist = $this->model->catelist($where,$page,$limit);
                $this->success['data']=$datalist['datalist'];
                $this->success['count']=$datalist['total'];
                $this->echojson($this->success);
                break;
            default:
                $this->display('cate_list.html');
                break;
        } 
        
    }

    /**
     * 合同類別添加
     * @param array $cateadd 
     * @param array $where 
     */ 
    public function cateadd(){
        $params = post();
        if($params){
            //post请求则保存
            $datainfo = $this->model->catesave($params);
            switch ($datainfo) {
            	case true:
            		$this->echoJson($this->success); 
            		break;
            	
            	default:
            		$this->echoJson($this->error); 
            		break;
            }
        }else{
            $datainfo =$this->model->cateinfo(get('id'));
            //定义变量datainfo输出
            $datainfo['id']=isset($datainfo['id'])?$datainfo['id']:'';
            $datainfo['title']=isset($datainfo['title'])?$datainfo['title']:'';
            $datainfo['titles']=isset($datainfo['titles'])?$datainfo['titles']:array();
            $datainfo['createtime']=isset($datainfo['createtime'])?$datainfo['createtime']:date('Y-m-d H:m',time());
            $datainfo['acode']=isset($datainfo['acode'])?$datainfo['acode']:'';
            $datainfo['pagecode']=isset($datainfo['pagecode'])?$datainfo['pagecode']:'';
            $datainfo['introduction']=isset($datainfo['introduction'])?$datainfo['introduction']:array();
            $datainfo['content']=isset($datainfo['content'])?$datainfo['content']:array();
            $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:1;
            $datainfo['imgurl']=isset($datainfo['imgurl'])?$datainfo['imgurl']:'';
            $datainfo['type']=isset($datainfo['type'])?$datainfo['type']:2;

            $langs=(new CommonModel)->getLang();
            $this->assign('langs',$langs);
            $this->assign('datainfo', $datainfo);
            $this->display('cate_add.html');
        }
    }

    /**
     * 类容添加
     * @param array $cateadd 
     * @param array $where 
     */ 
    // public function catedetail(){
    //     $this->display('detail.html');
    // }

    /**
     * 合同類別删除
     * @param array $catedel 
     * @param array $where 
     */
    public function catedel(){
        $datainfo=$this->model->catedel(post('id',false));
        switch ($datainfo) {
            case true:
                $this->echoJson($this->success); 
                break;
                
            default:
                $this->echoJson($this->error); 
                break;
        } 
    }

    /**
     * 文档列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list(){
        $post = post();
        switch (true) {
            case $post:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' a.dels<1 ';
                if(post('title')){
                    $where .= ' and a.title like "%'.post('title').'%"';
                }
                $datalist = $this->model->list($where,$page,$limit);
                $this->success['data']=$datalist['datalist'];
                $this->success['count']=$datalist['total'];
                $this->echojson($this->success);
                break;
            default:
                $this->display('list.html');
                break;
        } 
        
    }

    /**
     * 文档添加
     * @param array $cateadd 
     * @param array $where 
     */ 
    public function add(){
        $params = post();
        if($params){
            //post请求则保存
            $datainfo = $this->model->save($params);
            switch ($datainfo) {
                case true:
                    $this->echoJson($this->success); 
                    break;
                
                default:
                    $this->echoJson($this->error); 
                    break;
            }
        }else{
            $datainfo =$this->model->info(get('id'));
            //定义变量datainfo输出
            $datainfo['id']=isset($datainfo['id'])?$datainfo['id']:'';
            $datainfo['title']=isset($datainfo['title'])?$datainfo['title']:'';
            $datainfo['titles']=isset($datainfo['titles'])?$datainfo['titles']:array();
            $datainfo['cid']=isset($datainfo['cid'])?$datainfo['cid']:0;
            $datainfo['createtime']=isset($datainfo['createtime'])?$datainfo['createtime']:date('Y-m-d H:m',time());
            $datainfo['introduction']=isset($datainfo['introduction'])?$datainfo['introduction']:array();
            $datainfo['content']=isset($datainfo['content'])?$datainfo['content']:array();
            $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:1;
            $datainfo['imgurl']=isset($datainfo['imgurl'])?$datainfo['imgurl']:'';

            $langs=(new CommonModel)->getLang();
            $class =(new CommonModel)->getfilecate('type=1','id,title');
            $this->assign('langs',$langs);
            $this->assign('class', $class);
            $this->assign('datainfo', $datainfo);
            $this->display('add.html');
        }
    }
    /**
     * 类容添加
     * @param array $detail 
     * @param array $where 
     */ 
    public function detail(){
        $this->display('detail.html');
    }

    /**
     * 文档内容删除
     * @param array $del 
     * @param array $where 
     */
    public function del(){
        $datainfo=$this->model->del(post('id',false));
        switch ($datainfo) {
            case true:
                $this->echoJson($this->success); 
                break;
                
            default:
                $this->echoJson($this->error); 
                break;
        } 
    }
}
